package exttable.formgenerator;

import exttable.dbclasses.*;
import exttable.testselector.VolunteerSelectorView;
import utils.Localisation;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.util.List;


/**
 * Created by IntelliJ IDEA.
 * User: ADMIN
 * Date: 27.05.11
 * Time: 12:14
 * To change this template use File | Settings | File Templates.
 */
public class FormAddAdditionalDataForVolunteers {
    JFrame frame;
    JPanel panel;
    SpringLayout mainLayout;
    JLabel labelVolunteer;
    JComboBox jcbVolunteer;
    JLabel labelGroup;
    JList jListGroup;
    JScrollPane jScrollPane;
    JLabel labelDegreeOfInflammatory;
    JComboBox jcbDegreeOfInflammatory;
    JLabel labelProcessByLocalization;
    JComboBox jcbProcessByLocalization;

    JLabel labelDiseaseDuration;
    JPanel panelDiseaseDuration;
    JRadioButton rbDiseaseDurationYes;
    JRadioButton rbDiseaseDurationNo;
    ButtonGroup bgDiseaseDuration;

    JLabel labelNextButton;
    JButton nextButton;

    private Map<String, Object> mapForJTable;

    public FormAddAdditionalDataForVolunteers() {
        frame = new JFrame(Localisation.getMessage("menuItem.addAdditionalDataForVolunteers"));
        frame.setName("FormAddAdditionalDataForVolunteers");
        panel = new JPanel();
        labelVolunteer = new JLabel(Localisation.getMessage("menu.volunteer"));
        jcbVolunteer = new JComboBox(StaticMethods.getVolunteerList().toArray());
        labelDegreeOfInflammatory = new JLabel(Localisation.getMessage("labelDegreeOfInflammatory"));
        List<String> keyDegreeOfInflammatory = new ArrayList<String>();
        keyDegreeOfInflammatory.add("test10_instr_chest_x_radiography");
        keyDegreeOfInflammatory.add("degree_of_inflammatory_id");
        keyDegreeOfInflammatory.add("dic_degree_of_inflammatory");
        jcbDegreeOfInflammatory = new JComboBox(VolunteerSelectorView.getMapDicGeneral().get(keyDegreeOfInflammatory).toArray());

        labelProcessByLocalization = new JLabel(Localisation.getMessage("labelProcessByLocalization"));
        List<String> keyListProcessByLocalization = new ArrayList<String>();
        keyListProcessByLocalization.add("test10_instr_chest_x_radiography");
        keyListProcessByLocalization.add("process_by_localization_id");
        keyListProcessByLocalization.add("dic_process_by_localizations");
        jcbProcessByLocalization = new JComboBox(VolunteerSelectorView.getMapDicGeneral().get(keyListProcessByLocalization).toArray());

        labelDiseaseDuration = new JLabel(Localisation.getMessage("labelDiseaseDuration"));
        rbDiseaseDurationYes = new JRadioButton(Localisation.getMessage("form.yes"));
        rbDiseaseDurationNo = new JRadioButton(Localisation.getMessage("form.no"));
        rbDiseaseDurationNo.setSelected(true);
        bgDiseaseDuration = new ButtonGroup();
        bgDiseaseDuration.add(rbDiseaseDurationYes);
        bgDiseaseDuration.add(rbDiseaseDurationNo);
        panelDiseaseDuration = new JPanel();
        panelDiseaseDuration.add(rbDiseaseDurationYes);
        panelDiseaseDuration.add(rbDiseaseDurationNo);
        panel.add(labelVolunteer);
        panel.add(jcbVolunteer);
        panel.add(labelDegreeOfInflammatory);
        panel.add(jcbDegreeOfInflammatory);
        panel.add(labelProcessByLocalization);
        panel.add(jcbProcessByLocalization);
        panel.add(labelDiseaseDuration);
        panel.add(panelDiseaseDuration);

        labelNextButton = new JLabel();
        panel.add(labelNextButton);
        nextButton = new JButton(Localisation.getMessage("form.continue"));
        nextButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                AdditionalDataForVolunteers additionalDataForVolunteers = new AdditionalDataForVolunteers();
                additionalDataForVolunteers.setVolunteerId(((Volunteer) jcbVolunteer.getSelectedItem()).getVolunteerId());
                additionalDataForVolunteers.setVolunteerCode(jcbVolunteer.getSelectedItem().toString());
                additionalDataForVolunteers.setDegreeOfInflammatoryId(((DicGeneral) jcbDegreeOfInflammatory.getSelectedItem()).getDicId());
                additionalDataForVolunteers.setProcessByLocalizationId(((DicGeneral) jcbProcessByLocalization.getSelectedItem()).getDicId());
                additionalDataForVolunteers.setDiseaseDurationMoreThenYear(rbDiseaseDurationYes.isSelected());
//                new InsertAddAdditionalDataForVolunteers(additionalDataForVolunteers);

                Vector<Vector> rowData = new Vector<Vector>();
                mapForJTable = new LinkedHashMap<String, Object>();
                mapForJTable.put(Localisation.getMessage("menu.volunteer"), jcbVolunteer.getSelectedItem().toString());
                mapForJTable.put(Localisation.getMessage("labelDegreeOfInflammatory"), ((DicGeneral) jcbDegreeOfInflammatory.getSelectedItem()).toString());
                mapForJTable.put(Localisation.getMessage("labelProcessByLocalization"), ((DicGeneral) jcbProcessByLocalization.getSelectedItem()).toString());
                mapForJTable.put(Localisation.getMessage("labelDiseaseDuration"), rbDiseaseDurationYes.isSelected());
                for (Map.Entry<String, Object> res : mapForJTable.entrySet()) {
                    Vector<Object> tempVector = new Vector<Object>();
                    tempVector.add(res.getKey());
                    tempVector.add(res.getValue());
                    rowData.add(tempVector);
                }

                new InsertAddAdditionalDataForVolunteers(additionalDataForVolunteers, rowData);

            }
        });
        panel.add(nextButton);
        mainLayout = new SpringLayout();
        panel.setLayout(mainLayout);
        organiseLayout();
        frame.setSize(800, 600);
        frame.add(panel);
        frame.setVisible(true);
        frame.setName("FormAddAdditionalDataForVolunteers");

    }

    private void organiseLayout() {

        mainLayout.putConstraint(SpringLayout.WEST, labelVolunteer, 20, SpringLayout.WEST, panel);
        mainLayout.putConstraint(SpringLayout.NORTH, labelVolunteer, 10, SpringLayout.NORTH, panel);
        mainLayout.putConstraint(SpringLayout.WEST, jcbVolunteer, 250, SpringLayout.EAST, labelVolunteer);
        mainLayout.putConstraint(SpringLayout.NORTH, jcbVolunteer, 10, SpringLayout.NORTH, panel);

        mainLayout.putConstraint(SpringLayout.WEST, labelDegreeOfInflammatory, 20, SpringLayout.WEST, panel);
        mainLayout.putConstraint(SpringLayout.NORTH, labelDegreeOfInflammatory, 20, SpringLayout.SOUTH, labelVolunteer);
        mainLayout.putConstraint(SpringLayout.WEST, jcbDegreeOfInflammatory, 0, SpringLayout.WEST, jcbVolunteer);
        mainLayout.putConstraint(SpringLayout.NORTH, jcbDegreeOfInflammatory, 20, SpringLayout.SOUTH, jcbVolunteer);

        mainLayout.putConstraint(SpringLayout.WEST, labelProcessByLocalization, 20, SpringLayout.WEST, panel);
        mainLayout.putConstraint(SpringLayout.NORTH, labelProcessByLocalization, 20, SpringLayout.SOUTH, labelDegreeOfInflammatory);
        mainLayout.putConstraint(SpringLayout.WEST, jcbProcessByLocalization, 0, SpringLayout.WEST, jcbDegreeOfInflammatory);
        mainLayout.putConstraint(SpringLayout.NORTH, jcbProcessByLocalization, 20, SpringLayout.SOUTH, jcbDegreeOfInflammatory);

        mainLayout.putConstraint(SpringLayout.WEST, labelDiseaseDuration, 20, SpringLayout.WEST, panel);
        mainLayout.putConstraint(SpringLayout.NORTH, labelDiseaseDuration, 20, SpringLayout.SOUTH, jcbProcessByLocalization);
        mainLayout.putConstraint(SpringLayout.WEST, panelDiseaseDuration, 0, SpringLayout.WEST, jcbProcessByLocalization);
        mainLayout.putConstraint(SpringLayout.NORTH, panelDiseaseDuration, 20, SpringLayout.SOUTH, jcbProcessByLocalization);

        mainLayout.putConstraint(SpringLayout.WEST, labelNextButton, 20, SpringLayout.WEST, panel);
        mainLayout.putConstraint(SpringLayout.NORTH, labelNextButton, 20, SpringLayout.SOUTH, panelDiseaseDuration);
        mainLayout.putConstraint(SpringLayout.WEST, nextButton, 0, SpringLayout.WEST, panelDiseaseDuration);
        mainLayout.putConstraint(SpringLayout.NORTH, nextButton, 20, SpringLayout.SOUTH, panelDiseaseDuration);
    }

    private List<Group> convertArrayToListGroups() {
        List<Group> groupList = new ArrayList<Group>();
        for (Object obj : jListGroup.getSelectedValuesList()) {
            Group group = (Group) obj;
            groupList.add(group);
        }
        return groupList;
    }
}
